package com.orange.lockscreensettings.common;

import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.orange.lockscreensettings.common.Constants.Preference_Key;

public class SettingsUtils implements Preference_Key {
	private static final String			TAG						= "SettingsUtils";

	private static int					s_WallpaperIndex		= -1;
	private static boolean				sIsWallpaperFromGallery	= false;

	private static SharedPreferences	sharedPreferences;

	public static void init(Context context) {
		sharedPreferences = context.getSharedPreferences(
				Constants.PREFERENCES_SETTINGS_NAME,
				Context.MODE_WORLD_READABLE);

		s_WallpaperIndex = sharedPreferences.getInt(KEY_WALLPAPER_INDEX, -1);
		sIsWallpaperFromGallery = sharedPreferences.getBoolean(
				KEY_WALLPAPER_FROM_GALLERY, false);
	}

	public static int getIndexOfBG() {
		return s_WallpaperIndex;
	}

	public static void setIndexOfBG(int indexOfBG) {
		if (null != sharedPreferences) {
			Editor editor = sharedPreferences.edit();
			editor.putInt(KEY_WALLPAPER_INDEX, indexOfBG);
			editor.commit();
			s_WallpaperIndex = indexOfBG;
		}
	}

	public static boolean isWallpaperFromGallery() {
		return sIsWallpaperFromGallery;
	}

	public static void setIsWallpaperFromGallery(boolean fromGallery) {
		if (null != sharedPreferences) {
			Editor editor = sharedPreferences.edit();
			editor.putBoolean(KEY_WALLPAPER_FROM_GALLERY, fromGallery);
			editor.commit();
			sIsWallpaperFromGallery = fromGallery;
		}
	}

	public static int getScreenHeight(Activity activity) {
		Drawable phoneCallIcon = activity.getResources().getDrawable(
				android.R.drawable.stat_sys_phone_call);
		int notificationBarHeight = phoneCallIcon.getIntrinsicHeight();
		int fullScreenHeight = activity.getWindowManager().getDefaultDisplay()
				.getHeight();
		return fullScreenHeight - notificationBarHeight;
	}

	public static int getScreenWidth(Activity activity) {
		return activity.getWindowManager().getDefaultDisplay().getWidth();
	}

	public static boolean isPackageExist(Context context, String packageName) {
		final PackageManager packageManager = context.getPackageManager();
		List<PackageInfo> packageinfo = packageManager.getInstalledPackages(0);
		if (packageinfo != null) {
			for (int i = 0; i < packageinfo.size(); i++) {
				if (packageName.equals(packageinfo.get(i).packageName)) {
					return true;
				}
			}
		}
		return false;
	}

	public static void startActivityForResult(Activity activity, Intent intent,
			int requestCode) {
		if (intent == null) {
			return;
		}
		try {
			activity.startActivityForResult(intent, requestCode);
		} catch (ActivityNotFoundException e) {
			LogHelper.e(TAG, "startActivityForResult. ", e);
		}
	}
}
